<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/view/common/tag.jsp" %>
<html>
<head>
    <title>轮播图添加</title>

    <link rel="stylesheet" href="${baseurl}/lib/yueqain/css/yueqian.all.css" media="all">
    <link rel="stylesheet" href="${baseurl}/css/public.css" media="all">
    <style>
        .layui-upload-img {
            width: 110px;
            height: 110px;
            margin: 0 10px 10px 0;
        }
    </style>
</head>
<body class="white-bg">
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="carouselName" placeholder="请输入名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图片</label>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="filePathElem">点击上传图片</button>
                        <input type="hidden" name="carouselPath" id="carouselPath" autocomplete="off" class="layui-input">
                        <div class="layui-upload-list" style="margin-left: 110px">
                            <img class="layui-upload-img" id="filePath">
                            <p id="fileErrorText"></p>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sortNo" placeholder="请输入排序" class="layui-input"
                               lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <input type="text" name="carouseDes" number autocomplete="off" placeholder="请输入描述" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="yueqian-btn yueqian-btn-primary yueqian-btn-sm" lay-submit="" lay-filter="add">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="yueqian-btn yueqian-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script src="${baseurl}/lib/layui/layui.js" charset="utf-8"></script>
<script src="${baseurl}/lib/layui_custom/Hlayui.js" charset="utf-8"></script>
<script>

    layui.use(['form', 'jquery', 'upload', 'element', 'layer', 'laydate', 'layerCustom'], function () {
        var element = layui.element;
        $ = layui.jquery,
            upload = layui.upload,
            layer = layui.layer,
            layerCustom = layui.layerCustom,
            laydate = layui.laydate,
            form = layui.form;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#filePathElem'
            , url: BaseUrl + 'file/upload' //文件上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#filePath').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //设置到隐藏的 input中
                $("#carouselPath").val(res.data.url);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var fileErrorText = $('#fileErrorText');
                fileErrorText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                fileErrorText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });


        /*表单验证*/
        form.verify({

        });


        /*表单提交*/
        form.on('submit(add)', function (data) {
            var url = BaseUrl + 'carousel/save';
            //保存接口
            $.post(url, data.field, function (result) {
                if (result.success) {
                    layer.msg(result.msg, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    })
                } else {
                    layer.msg(result.msg)
                    return;
                }
            }, 'json');

            return false;
        });
    });
</script>
</body>
</html>
